package org.lex.notepad.util.laf;

import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import org.lex.string.StringUtils;

public class InstalledLookAndFeelsList extends JList {
	private String[] lnfNames = null;
	private String[] lnfClassNames = null;

	public InstalledLookAndFeelsList() {
		super();
		this.detectInstalledLookAndFeels();
		this.initializeComponents();
	}

	public String getSelectedLookAndFeelName() {
		int selected = this.getSelectedIndex();
		if (-1 == selected)
			return "";
		return this.lnfNames[selected];
	}

	public String getSelectedLookAndFeelClassName() {
		int selected = this.getSelectedIndex();
		if (-1 == selected)
			return "";
		return this.lnfClassNames[selected];
	}

	public boolean setSelectedLookAndFeel(String lnfName) {
		if ("".equals(lnfName))
			return false;
		for (int i = 0; i < this.lnfNames.length; i++)
			if (lnfName.equalsIgnoreCase(lnfNames[i])) {
				this.setSelectedIndex(i);
				return true;
			}
		return false;
	}

	public boolean setSelectedLookAndFeelClass(String lnfClassName) {
		if (StringUtils.isNullOrTrimEmpty(lnfClassName)) {
			return false;
		}
		for (int i = 0; i < this.lnfClassNames.length; i++)
			if (lnfClassName.equalsIgnoreCase(lnfClassNames[i])) {
				this.setSelectedIndex(i);
				return true;
			}
		return false;
	}

	private void detectInstalledLookAndFeels() {
		LookAndFeelInfo[] all = UIManager.getInstalledLookAndFeels();
		lnfNames = new String[all.length];
		lnfClassNames = new String[all.length];
		for (int i = 0; i < all.length; i++) {
			LookAndFeelInfo info = all[i];
			lnfNames[i] = info.getName();
			lnfClassNames[i] = info.getClassName();
		}
	}

	private void initializeComponents() {
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setListData(lnfNames);
	}
}
